<?php
namespace app\admin\controller;
use app\model\User as UserModel;
use think\Db;


class User extends Base
{
    public function __construct(){
        $model = new UserModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '活动';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function assignFields(){
        $model = $this->model;


        $fields = [
            'vip_time'=>['name'=>'VIP有效期','type'=>'date'],
            'agent_time'=>['name'=>'AGENT有效期','type'=>'date'],
            'api_key'=>['name'=>'API KEY','type'=>'text'],
            'api_secret'=>['name'=>'API SECRET','type'=>'text'],
        ];
        $valids = createPageValid($fields);
        $this->assign([
            'fields'=>$fields,
            'valids'=>$valids
        ]);

    }

    public function assignSearchs(){
        $model = $this->model;
        $search_params = [
            [
                ['key'=>'nickname','name'=>'昵称','type'=>'text'],
                ['key'=>'mobile','name'=>'手机','type'=>'text'],
                ['key'=>'source','name'=>'来源','type'=>'select1_1','params'=>$model->getSourceList()],
                ['key'=>'status','name'=>'状态','type'=>'select1_1','params'=>$model->getStatusList()],
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);
    }

    public function index(){
        $model = $this->model;
        $map_params = [
            ['key'=>'nickname','type'=>'like'],
            ['key'=>'mobile','type'=>'like'],
            ['key'=>'source','type'=>'='],
            ['key'=>'status','type'=>'='],
        ];
        $map = getSearchParam($map_params);

        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',20)
        ];
        // 显示筛选
        $this->assignSearchs();

        $list = $model->where($map)->order('id','desc')->paginate($page_param);
        $page = $list->render();
        $list = $list->toarray();
        foreach ($list['data'] as $k => $vo) {
            if (!empty($vo['invite_id'])) {
                $list['data'][$k]['invite_name'] = Db::name('user')->where('id',$vo['invite_id'])->value('nickname');
            }
        }
        return view('index', [
            'list'=>$list,
            'page'=>$page
        ]);
    }

    public function add(){
        if (request()->isPost()) {
            $fields = [
                'must'=>[],
                'nomust'=>['vip_time','agent_time','api_key','api_secret']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
        }

        return parent::saveData($save_data);
    }

    public function edit(){
        $id = input('id');
        if (empty($id)) { $this->error('id不能为空'); }
        
        $model = $this->model;
        if (request()->isPost()) {
            $fields = [
                'must'=>[],
                'nomust'=>['vip_time','agent_time','api_key','api_secret']
            ];
            $save_data = paramFilter(request()->param(),$fields);
            if (!empty($save_data['error_msg'])) {
                $this->error($save_data['error_msg']);
            }
        }

        return parent::updateData($id,$save_data);
    }

    public function export(){
        $model = $this->model;
        $map_params = [
            ['key'=>'nickname','type'=>'like'],
            ['key'=>'mobile','type'=>'like'],
            ['key'=>'source','type'=>'='],
            ['key'=>'status','type'=>'='],
        ];
        $map = getSearchParam($map_params);

        $list = $model->where($map)->order('id','asc')->select();
        $list = $list->toarray();
        foreach ($list as $k => $vo) {
            if (!empty($vo['invite_id'])) {
                $list['data'][$k]['invite_name'] = Db::name('user')->where('id',$vo['invite_id'])->value('nickname');
            }
        }

        $data = [];
        $title = ['昵称', '姓名', '手机','OPENID','积分','邀请人','来源','状态','注册时间','VIP有效期'];
        foreach ($list as $k => $vo) {
            $tmp = [
                $vo['nickname'],$vo['truename'],$vo['mobile'],$vo['openid'],$vo['integral'],$vo['invite_name'],
                $model->getSourceCn($vo['source']),
                $model->getStatusCn($vo['status']),
                timeDef($vo['ctime']),
                $vo['vip_time']
            ];

            foreach ($tmp as $t_k => $t_v) {
                $tmp[$t_k] = str_replace([',',"\n","\r"], '，', $t_v);
            }
            $data[] = $tmp;
        }

        exportCsv(date('YmdHis'),$title,$data);
    }




   
}
